var app = getApp();
var util = require('../../../utils/util.js');

Page({
  data: {
    detId: "",
    bannerList: "",
    imgUrl: app.globalData.imgUrl,
    systemInfo: {},
    latitude: "",
    longitude: "",
    markers: "",
    mapName: "",
    mapSite: "",
    cart_exists: "",
    padBtm: "25",
    scrollFlag: true,
    topid: 0
  },
  onLoad: function(e) {
    var that = this;
    if (e.topid) {
      var topid = e.topid;
      that.setData({
        topid: topid
      })
      console.log("topidd", topid)
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          systemInfo: res,
          navH: app.globalData.navHeight
        });
        if (res.model == "iPhone X") {
          that.setData({
            padBtm: "50",
          })
        }
      }
    });
    that.setData({
      detId: e.id
    })
    that.getHotelDet();
  },

  ordernow() {
    var that = this;
    //判断token
    if (wx.getStorageSync('token') == '') {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    } else {
      that.checkToken()
      wx.navigateTo({
        url: '/pages/order/info/order-info?name=hotel&id=' + that.data.detId + '&topid=' + that.data.topid ,
      })
    }
  },
  checkToken: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/token/check",
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function (res) {
        if (res.statusCode === 200) {
          console.log(res);
          //监测token是否过期，如果没过期就用缓存取出来的，如果过期就用刷新后的token
          if (res.data.code != 1) {
            that.refreshToken();
          } else {
          }
        }
      }
    });
  },
  refreshToken: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/token/refresh",
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function (res) {
        if (res.statusCode === 200) {
          //刷新后修改全局的token
          wx.setStorageSync('token', res.data.data.token);
        }
      }
    });
  },
  
  onShareAppMessage() {
    var that = this;
    return {
      title: '我是海花岛业主，我为漫屿筱筑代言！',
      //path: '/pages/hotel/detail/hotel-det?id=' + that.data.detId
      //path: 'pages/index/index?url=/pages/hotel/detail/hotel-det?id=' + that.data.detId
      path: 'pages/index/index?url=/pages/hotel/detail/hotel-det&id=' + that.data.detId
    }
  },
  getCategoryDetail: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/index/categoryDetail",
      data: {
        id: that.data.mapScale
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var typeData = res.data.data;
          console.log(res);
          console.log(typeData);
          that.setData({
            mapNum: typeData.description,
          })
        }
      },
    });
  },
  getHotelDet: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/index/house",
      data: {
        id: that.data.detId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var hotelData = res.data.data;
          var hotelName = hotelData.house_name;
          var hotelScale = hotelData.scale;
          var hotelSite = hotelData.province_text + hotelData.city_text + hotelData.area_text + hotelData.address;
          var latitudeNum = parseFloat(hotelData.latitude);
          var longitudeNum = parseFloat(hotelData.longitude);
          var hotelBanner = hotelData.pics.split(',');
          var hotelBody = hotelData.body;
          var hotelTextCode = util.baseDecode(hotelBody); //base64解码
          that.setData({
            hotelDet: hotelData,
            cart_exists: hotelData.cart_exists,
            bannerList: hotelBanner,
            hotelText: hotelTextCode,
            latitude: latitudeNum,
            longitude: longitudeNum,
            mapName: hotelName,
            mapScale: hotelScale,
            mapSite: hotelSite,
            markers: [{
              iconPath: '../../../images/map.png',
              id: 0,
              latitude: latitudeNum,
              longitude: longitudeNum,
              width: 50,
              height: 50
            }],
          });
          that.getCategoryDetail();
        }
      },

    });
  },
  onPageScroll: function(e) {
    var that = this;
    if (e.scrollTop >= 140) {
      that.setData({
        scrollFlag: false
      });
    } else {
      that.setData({
        scrollFlag: true
      });
    }
  },
  navBack: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  getLocation: function() {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        wx.openLocation({
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          name: that.data.mapName,
          address: that.data.mapSite
        })
      }
    })
  },
  cartOperate: function(e) {
    var that = this;
    var proId = e.currentTarget.dataset.id;
    if (that.data.cart_exists == 0) {
      app.cartAdd(proId, 1);
      that.setData({
        cart_exists: 1
      })
    } else if (that.data.cart_exists == 1) {
      app.cartDelete(proId, 1);
      that.setData({
        cart_exists: 0
      })
    }
  }
})